package com.qf.web.view;

import com.qf.pojo.Emp;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 *  用来展示 员工列表
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@WebServlet("/showEmpList")
public class ShowEmpListServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter writer = resp.getWriter();
        writer.println("<html>");
        writer.println("<head>");
        writer.println("<title>员工列表</title>");
        writer.println("</head>");
        writer.println("<body>");
        ServletContext servletContext = req.getServletContext();
        Object count = servletContext.getAttribute("count");

        writer.println("当前在线人数：" + count);

        writer.println("<table align='center' border='1' cellspacing='0' bordercolor='red'>");
        writer.println("<tr>");
        writer.println("<th>id</th>");
        writer.println("<th>姓名</th>");
        writer.println("<th>工资</th>");
        writer.println("<th>年龄</th>");
        writer.println("<th>操作</th>");
        writer.println("</tr>");

        Object emps = req.getAttribute("emps");

        if(emps == null ) {
            writer.println("<tr>");
            writer.println("<td colspan='5'>员工列表为空</td>");
            writer.println("</tr>");
        }else {
            List<Emp> empList = (List<Emp>) emps;
            if(empList.size() == 0) {
                writer.println("<tr>");
                writer.println("<td colspan='5'>员工列表为空</td>");
                writer.println("</tr>");
            }else {

                for (Emp emp : empList) {
                    writer.println("<tr>");
                    writer.println("<td>"+emp.getId()+"</td>");
                    writer.println("<td>"+emp.getName()+"</td>");
                    writer.println("<td>"+emp.getSalary()+"</td>");
                    writer.println("<td>"+emp.getAge()+"</td>");
                    writer.println("<td><a href='deleteEmp?id="+emp.getId()+"'>删除</a>   <a href='selectOneEmp?id="+emp.getId()+"'>修改</a></td>");
                    writer.println("</tr>");
                }
            }

        }

        writer.println("</table>");
        writer.println("</body>");
    }
}
